//
//  GameLayer.m
//  Coffee
//
//  Created by sabhanayagam b on 4/29/11.
//  Copyright 2011 Cognizant. All rights reserved.
//

#import "GameLayer.h"
#import "GameManager.h"
#import "AllShapes.h"

@implementation GameLayer
@synthesize transitVector;

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init] )) {
		self.isTouchEnabled = YES;
		
		friction = 0.98f;
		bounce   = 0.8f;
		gravity  = 0.9f;
		winSize = [CCDirector sharedDirector].winSize;
		/*
		player = [CCSprite spriteWithFile:@"user.png"];
		[self addChild:player];
		[player setAnchorPoint:ccp(0.0f , 0.0f)];
		player.position = ccp(100.0f , 100.0f);
		*/
		//Swap Shapes
		changeShape = YES;
		isCollision = NO;
		
		polyz = [[Square alloc] initWithSquareSize:CGSizeMake(10.0f, 48.0f)];
		[polyz translateVelocity:[Vector2D createVectorWithComponentX:400.0f Y:100.0f]];
		
		polya = [[Square alloc]initWithSquareSize:CGSizeMake(10.0f, 10.0f)];
		[polya translateVelocity:[Vector2D createVectorWithComponentX:100.0f Y:0.0f]];
		
		polyb = [[Square alloc] initWithSquareSize:CGSizeMake(100.0f, 100.0f)];
		[polyb translateVelocity:[Vector2D createVectorWithComponentX:300.0f Y:0.0f]];
		
		velocity      = [[Vector2D alloc] initWithComponentX:2.0f Y:0.0f];
		transitVector = [Vector2D createVector];

	}
	
	
	return self;
}
- (void)draw {
	// ...
	
	// draw a simple line
	// The default state is:
	// Line Width: 1
	// color: 255,255,255,255 (white, non-transparent)
	// Anti-Aliased
	glEnable(GL_LINE_SMOOTH);
	glColor4f(0.0, 0.0, 0.0, 1.0);  
    glLineWidth(2.0f);
	
	ccDrawPoly(polyz.points , polyz.count , YES);
	ccDrawPoly(polya.points , polya.count , YES);

	velocity.vy -= gravity;
	/*if (!isCollision) {
		
	}*/
	[polyz translateVelocity:velocity];
	velocity.vx *= friction;
	if ([polyz isCollidingWith:polya translationVector:transitVector]) {
		[polyz translateVelocity:transitVector];
	}
	
	
	
	if (polyz.points[0].y <= 0.0f ) {
		velocity.vy = 0;
		[polyz restInPeace];
	}
	
	if (polyz.center.x > (winSize.width - polyz.size.width) || polyz.center.x < polyz.size.width ) {
		velocity.vx *= -friction;
	}
	
}


//Touch
-(void) registerWithTouchDispatcher
{
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent *)event
{
	lastTouchLocation = [GameManager locationFromTouch:touch ];
	changeShape = !changeShape;

	if (lastTouchLocation.x > HALF_WIDTH) {
		velocity.vx = 8.9f;
		//velocity.vy = 4.9f;
		if(lastTouchLocation.y > HALF_HEIGHT)
			velocity.vy = 8.0f;
	}else {
		velocity.vx = -8.9f;
		//velocity.vy = 4.9f;
		if(lastTouchLocation.y > HALF_HEIGHT)
			velocity.vy = 8.0f;
	}
	
	// Always swallow touches, GameLayer is the last layer to receive touches.
	return YES;
}

-(void) ccTouchMoved:(UITouch*)touch withEvent:(UIEvent *)event
{
	
}

-(void) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event
{
	// Move the game layer back to its designated position.
	// Create the actions
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	[polyz release];
	[polya release];
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	//[player release];
	// don't forget to call "super dealloc"
	[super dealloc];
}
@end
